package com.zufe.LibrarySystem;

import java.util.ArrayList;
import java.util.Scanner;

public class LibrarySystem {
    public static void main(String[] args) {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        ArrayList<Book> books = new ArrayList<>();
        while (flag) {
            System.out.println("--------------图书馆管理系统--------------");
            System.out.println("1.添加图书");
            System.out.println("2.查看所有图书");
            System.out.println("3.查询图书");
            System.out.println("4.删除图书");
            System.out.println("5.修改图书");
            System.out.println("6.退出");
            System.out.println("请选择：");
            String choice = sc.next();
            switch (choice) {
                case "1" -> addBook(books);
                case "2" -> showAllBooks(books);
                case "3" -> queryBook(books);
                case "4" -> deleteBook(books);
                case "5" -> updateBook(books);
                case "6" -> {
                    System.out.println("退出");
                    flag = false;
                }
                default -> System.out.println("输入有误，请重新输入");
            }
        }
    }

    public static void addBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入图书编号：");
            String id = sc.next();
            if (contains(books, id)) {
                System.out.println("图书编号已存在，请重新输入");
                continue;
            } else {
                System.out.println("请输入图书名称：");
                String title = sc.next();
                System.out.println("请输入图书作者：");
                String author = sc.next();
                System.out.println("请输入图书价格：");
                String price = sc.next();
                Book book = new Book(id, title, author, price);
                books.add(book);
                System.out.println("添加成功");
                break;
            }
        }
    }

    public static void showAllBooks(ArrayList<Book> books) {
        System.out.println("查看所有图书");
        for (Book book : books) {
            System.out.println(book.getId() + "\t" + book.getTitle() + "\t" + book.getAuthor() + "\t" + book.getPrice());
        }
    }

    public static void queryBook(ArrayList<Book> books) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要查看的书的名称：");
            String title = sc.next();
            int index = getIndexByTitle(books, title);
            if (index == -1) {
                System.out.println("没有找到该书，请重新输入");
                continue;
            }else{
                Book book = books.get(index);
                System.out.println("编号\t名称\t作者\t价格");
                System.out.println(book.getId() + "\t" + book.getTitle() + "\t" + book.getAuthor() + "\t" + book.getPrice());
                break;
            }
        }
    }

    public static void deleteBook(ArrayList<Book> books) {
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要删除的书的编号：");
            String id = sc.next();
            int index = getIndex(books, id);
            if(index == -1){
                System.out.println("没有找到该书，请重新输入");
                continue;
            }else{
                books.remove(index);
                System.out.println("删除成功");
                break;
            }
        }
    }

    public static void updateBook(ArrayList<Book> books) {
       while (true){
              Scanner sc = new Scanner(System.in);
              System.out.println("请输入要修改的书的编号：");
              String id = sc.next();
              int index = getIndex(books, id);
              if(index == -1){
                System.out.println("没有找到该书，请重新输入");
                continue;
              }else{
                System.out.println("请输入新的图书名称：");
                String title = sc.next();
                System.out.println("请输入新的图书作者：");
                String author = sc.next();
                System.out.println("请输入新的图书价格：");
                String price = sc.next();
                Book book = new Book(id, title, author, price);
                books.set(index, book);
                System.out.println("修改成功");
                break;
              }
       }
    }

    public static int getIndex(ArrayList<Book> books, String id) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean contains(ArrayList<Book> books, String id) {
        return getIndex(books, id) != -1;
    }

    public static int getIndexByTitle(ArrayList<Book> books, String title) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(title)) {
                return i;
            }
        }
        return -1;
    }
}

